package com.softtown.modules.dapai.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.modules.dapai.dao.CompWarehouseOrderDao;
import com.softtown.modules.dapai.dto.CompWarehouseOrderAddDTO;
import com.softtown.modules.dapai.dto.CompWarehouseOrderDTO;
import com.softtown.modules.dapai.dto.CompWarehouseOrderDetailDTO;
import com.softtown.modules.dapai.entity.CompMacEntity;
import com.softtown.modules.dapai.entity.CompWarehouseOrderDetailEntity;
import com.softtown.modules.dapai.entity.CompWarehouseOrderEntity;
import com.softtown.modules.dapai.service.CompWarehouseOrderDetailService;
import com.softtown.modules.dapai.service.CompWarehouseOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @Author: Ding Ya Yong
* @Description: 售后申请单
* @date: 2022/1/2 11:38
* @Param:
* @return:
*/

@Service
public class CompWarehouseOrderServiceImpl extends
        BaseServiceImpl<CompWarehouseOrderDao, CompWarehouseOrderEntity> implements CompWarehouseOrderService {


    @Resource
    private CompWarehouseOrderDetailService compWarehouseOrderDetailService;


    @Override
    public PageInfo<CompWarehouseOrderDTO> getWarehouseOrderPageList(CompWarehouseOrderDTO compWarehouseOrderDTO) {
        Integer pageSize = compWarehouseOrderDTO.getPageSize();
        Integer page = compWarehouseOrderDTO.getPage();
        if (pageSize > 0 && page > 0) {
            PageHelper.startPage(page, pageSize);
        }
        List<CompWarehouseOrderDTO> compWarehouseOrderDTOList =
                baseDao.getWarehouseOrderPageList(compWarehouseOrderDTO.getCompId(), compWarehouseOrderDTO.getParam());
        return  new PageInfo<>(compWarehouseOrderDTOList);
    }

    @Override
    public PageInfo<CompWarehouseOrderDetailDTO> getWarehouseOrderDetailPageList(CompWarehouseOrderDetailDTO compWarehouseOrderDetailDTO) {
        Integer pageSize = compWarehouseOrderDetailDTO.getPageSize();
        Integer page = compWarehouseOrderDetailDTO.getPage();
        if (pageSize > 0 && page > 0) {
            PageHelper.startPage(page, pageSize);
        }
        List<CompWarehouseOrderDetailDTO> compWarehouseOrderDTOList =
                baseDao.getWarehouseOrderDetailPageList(compWarehouseOrderDetailDTO.getCompId(), compWarehouseOrderDetailDTO.getParam());
        return  new PageInfo<>(compWarehouseOrderDTOList);
    }

    @Override
    public CompWarehouseOrderAddDTO getWarehouseOrderById(Long compId, Long id) {
        CompWarehouseOrderAddDTO compWarehouseOrderAddDTO = new CompWarehouseOrderAddDTO();
        compWarehouseOrderAddDTO.setCompWarehouseOrderEntity(baseDao.getWarehouseOrderById(compId, id));
        compWarehouseOrderAddDTO.setCompWarehouseOrderDetailEntityList(baseDao.getWarehouseOrderDetailList(compId, id));
        return compWarehouseOrderAddDTO;
    }

    @Override
    @Transactional
    public void addWarehouseOrder(CompWarehouseOrderAddDTO CompWarehouseOrderAddDTO) {
        CompWarehouseOrderEntity compWarehouseOrderEntity =ConvertUtils
                .sourceToTarget(CompWarehouseOrderAddDTO.getCompWarehouseOrderEntity(), CompWarehouseOrderEntity.class);
        insert(compWarehouseOrderEntity);
        CompWarehouseOrderAddDTO.getCompWarehouseOrderDetailEntityList().forEach(item ->{
            item.setWarehouseOrderId(compWarehouseOrderEntity.getId());
            item.setCompId(compWarehouseOrderEntity.getCompId());
        });
        compWarehouseOrderDetailService.insertBatch(ConvertUtils
                .sourceToTarget(CompWarehouseOrderAddDTO.getCompWarehouseOrderDetailEntityList(), CompWarehouseOrderDetailEntity.class));

    }

    @Override
    @Transactional
    public void updateMacStock(CompMacEntity compMacEntity) {
        baseDao.updateMacStock(compMacEntity.getCompId(), compMacEntity.getId(), compMacEntity.getStock());
    }

    @Override
    @Transactional
    public void updateWarehouseOrder(CompWarehouseOrderAddDTO CompWarehouseOrderAddDTO) {
        CompWarehouseOrderEntity compWarehouseOrderEntity =ConvertUtils
                .sourceToTarget(CompWarehouseOrderAddDTO.getCompWarehouseOrderEntity(), CompWarehouseOrderEntity.class);
        updateById(compWarehouseOrderEntity);
        baseDao.deleteDetail(compWarehouseOrderEntity.getCompId(), compWarehouseOrderEntity.getId());
        CompWarehouseOrderAddDTO.getCompWarehouseOrderDetailEntityList().forEach(item ->{
            item.setWarehouseOrderId(compWarehouseOrderEntity.getId());
            item.setCompId(compWarehouseOrderEntity.getCompId());
        });
        compWarehouseOrderDetailService.insertBatch(ConvertUtils
                .sourceToTarget(CompWarehouseOrderAddDTO.getCompWarehouseOrderDetailEntityList(), CompWarehouseOrderDetailEntity.class));
    }

    @Override
    public void deleteWarehouseOrder(CompWarehouseOrderDTO compWarehouseOrderDTO) {
        baseDao.deleteWarehouseOrder(compWarehouseOrderDTO.getCompId(), compWarehouseOrderDTO.getId());
        baseDao.deleteDetail(compWarehouseOrderDTO.getCompId(), compWarehouseOrderDTO.getId());
    }
}
